import { _decorator, Node, Vec3 } from 'cc';
import { EventMgr } from '../../../../Framework/Scripts/Managers/EventMgr';
import { poolManager } from '../../../../Framework/Scripts/Managers/poolManager';
import { ResMgr } from '../../../../Framework/Scripts/Managers/ResMgr';
import { SoundMgr } from '../../../../Framework/Scripts/Managers/SoundMgr';
import myUtil from '../../../../Framework/Scripts/Utils/myUtil';
import { constant } from '../../constant';
import { GameApp } from '../../GameApp';
import { Arrow } from '../arrow/arrow';
import { arrowSingle } from '../arrow/arrowSingle';
import { enemyBase } from '../enemyBase';
import { playerBase } from '../playerBase';
import { tornado } from '../playerSkill/tornado';
const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('player01')
export class player01 extends playerBase {
    // private ndArrow: Node = null//玩家射出的箭
    private is3Skill: boolean = false//发送三连射技能
    public jiguangSkill: Node = null
    public longjuanfengSkill: Node = null
    public hudunSkill: Node = null
    private jisu: number = 0
    private oldJsu: number = 30
    start() {

    }
    //创建弓箭
    private createArrow(Gui: string, assetName: string): Node {
  

        const n: Node = poolManager.instance.getNode(ResMgr.Instance.getAsset(Gui, assetName), this.node.parent)





        n.setWorldPosition(this.arrowInHand.worldPosition);

        return n

    }


    //普通攻击 射箭
    public attickEnemy() {

        this.nowEnemy = null
        if (this.isDie) {
            return
        }


        if (!this.is3Skill) {
            if (this.isMove || this.onlyRotate) {

                this.isLookAt = false

                return
            }
        } else {
            //是三连击
            // if (this.nowEnemy) {
            //     this.isLookAt = true
            // }

            // console.log("三连击触发");
            this.PlayerModel.stopAni()
            this.PlayerModel.playAni("attack", true, null, 5);

            return
        }




        this.nowEnemy = this.findEnemy()




        if (this.nowEnemy) {
            this.isLookAt = true

            //播放攻击动画
            this.PlayerModel.stopAni()
            this.PlayerModel.playAni("attack", true, null, this.attackSpeed);
        } else {
            this.isLookAt = false

            this.PlayerModel.stopAni()
            this.PlayerModel.playAni("idle", true, null, 1)

        }







    }
    onDestroy() {
        super.onDestroy()
    }

    public init() {

        this.jisu = this.oldJsu




        const f: number = GameApp.Instance.playerData.config.jisu.level * 2

        this.jisu += f
        //  console.log("急速"+this.jisu);
        super.init()
    }

    //动画回调射箭
    public attickCallback() {



        this.arrowInHand.active = true
        //  console.log("动画播放完了回调！");

        this.isLookAt = false

        const ndArrow: Node = this.createArrow("prefab", "arrow/" + GameApp.Instance.arrowName)
        //初始化所有弓箭

        if (ndArrow) {
            ndArrow.eulerAngles = new Vec3(0, this._degree, 0)
            ndArrow.children.forEach((ndArrowItem: Node) => {

                let scriptArrowItem: Arrow = ndArrowItem.getComponent(Arrow) as Arrow;

                let scriptArrowItem2: arrowSingle = ndArrowItem.getComponent(arrowSingle) as arrowSingle;



                if (scriptArrowItem) {


                    scriptArrowItem.init(this.node, this.jisu, this.node.getWorldPosition(), false);

                } else {
                    //   console.log("是单支");

                    if (this.nowEnemy && !this.nowEnemy.getComponent(enemyBase).isDie) {
                        scriptArrowItem2.init(this.node, this.jisu, this.node.getWorldPosition(), this.nowEnemy, false);

                    } else {
                        scriptArrowItem2.init(this.node, this.jisu, this.node.getWorldPosition(), null, false);

                    }


                }







            })
        }
        
        this.nowEnemy = null
         SoundMgr.Instance.playSound(ResMgr.Instance.getAsset("Sounds", "loose"))

        this.attickEnemy()
    }

    private skill1() {
        //    console.log("三连击");

        EventMgr.Instance.Emit(constant.EVENT_NAME.allSkillBtnEnabled, true)

        // this.isLookAt = true
        this.is3Skill = true
        this.isStopMove = true
        this.attickEnemy()

        this.scheduleOnce(() => {
            //    console.log("三连击结束");

            EventMgr.Instance.Emit(constant.EVENT_NAME.allSkillBtnEnabled, false)
            this.is3Skill = false
            this.isStopMove = false

            this.attickEnemy()

        }, 3)



    }

    private skill2() {


        if (this.isDie) {
            return
        }

        this.PlayerModel.stopAni()
        this.PlayerModel.playAni("idle", true, null, 1)
        this.jiguangSkill = null
        this.jiguangSkill = poolManager.instance.getNode(ResMgr.Instance.getAsset("prefab", "playerSkill/Archer/jiguang_skill"), this.node)
        this.jiguangSkill.position = new Vec3(0, 1, -1)
        this.node.forward = this.skillForward
        this.jiguangSkill.forward = this.skillForward


        this.onlyRotate = true
        EventMgr.Instance.Emit(constant.EVENT_NAME.allSkillBtnEnabled, true)
        this.scheduleOnce(() => {
            EventMgr.Instance.Emit(constant.EVENT_NAME.allSkillBtnEnabled, false)
            if (this.jiguangSkill) {

                poolManager.instance.putNode(this.jiguangSkill)
                this.jiguangSkill = null


            }
            this.isMove = false
            this.onlyRotate = false
            this.attickEnemy()
        }, 3)
        //  console.log("激光");
    }
    private skill3() {
        SoundMgr.Instance.playSound(ResMgr.Instance.getAsset("Sounds", "tornado"))

        
        //  console.log("火焰风暴", this.skillPos);
        this.longjuanfengSkill = null
        this.longjuanfengSkill = poolManager.instance.getNode(ResMgr.Instance.getAsset("prefab", "playerSkill/Archer/longjuanf_skill"), GameApp.Instance.skillRoot)
        this.longjuanfengSkill.setScale(new Vec3(2, 2, 2))
        this.longjuanfengSkill.setWorldPosition(this.node.worldPosition)

        const ts: tornado = this.longjuanfengSkill.getComponent(tornado)
        if (ts) {
            ts.moveToPos(this.skillPos)
        }





    }
    private skill4() {
        SoundMgr.Instance.playSound(ResMgr.Instance.getAsset("Sounds", "invincible"))

        this.hudunSkill = null
        this.hudunSkill = poolManager.instance.getNode(ResMgr.Instance.getAsset("prefab", "playerSkill/Archer/shieldShow"), this.node)
        this.hudunSkill.setPosition(0, 0.3, 0)
        this.isWuDI = true
        myUtil.playEffect(this.hudunSkill, true, true, 1, true, 1, () => {
            this.hudunSkill = null
            this.hudunSkill = poolManager.instance.getNode(ResMgr.Instance.getAsset("prefab", "playerSkill/Archer/shield"), this.node)
            this.hudunSkill.setPosition(0, 0.3, 0)
        })
        this.scheduleOnce(() => {
            if (this.hudunSkill) {
                poolManager.instance.putNode(this.hudunSkill)
                this.hudunSkill = null
                this.isWuDI = false
            }

        }, 6)

       // console.log("护盾");
    }
    onLoad() {
        super.onLoad()
        //  console.log(UserData.Instance.localData);





        if (GameApp.Instance.playerData["skills"]["skill1"]["minLevel"] > GameApp.Instance.level) {
            //  console.log("不能绑定");

            //不能绑定
        } else {
            GameApp.Instance.playerData["skills"]["skill1"]["skillFunCB"] = this.skill1.bind(this)
            //  console.log(GameApp.Instance.playerData["skills"]["skill1"]["minLevel"]); 
        }

        if (GameApp.Instance.playerData["skills"]["skill2"]["minLevel"] > GameApp.Instance.level) {
            // console.log("不能绑定");
            //不能绑定
        } else {
            GameApp.Instance.playerData["skills"]["skill2"]["skillFunCB"] = this.skill2.bind(this)
            //  console.log(GameApp.Instance.playerData["skills"]["skill2"]["minLevel"]);
        }

        if (GameApp.Instance.playerData["skills"]["skill3"]["minLevel"] > GameApp.Instance.level) {
            //  console.log("不能绑定");
            //不能绑定
        } else {
            GameApp.Instance.playerData["skills"]["skill3"]["skillFunCB"] = this.skill3.bind(this)
            // console.log(GameApp.Instance.playerData["skills"]["skill3"]["minLevel"]);
        }


        if (GameApp.Instance.playerData["skills"]["skill4"]["minLevel"] > GameApp.Instance.level) {
            //  console.log("不能绑定");
            //不能绑定
        } else {
            GameApp.Instance.playerData["skills"]["skill4"]["skillFunCB"] = this.skill4.bind(this)
            //  console.log(GameApp.Instance.playerData["skills"]["skill4"]["minLevel"]);
        }



    }

    update(dt: number) {
        super.update(dt)
    }
}


